//
//  MineViewController.m
//  GiftSay
//
//  Created by dllo on 16/1/6.
//  Copyright © 2016年 liyinkai. All rights reserved.
//

#import "MineViewController.h"
#import "SwitchTableViewCell.h"
#import "ListViewController.h"
#import "ConnectOurViewController.h"
#import "AboutOurViewController.h"

@interface MineViewController ()<UITableViewDataSource,UITableViewDelegate,SwitchTableViewCellDelegate>

@property (nonatomic,strong) UITableView *table;

@end

@implementation MineViewController

-(void)viewWillAppear:(BOOL)animated {
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"NightMode"] == YES) {
        self.navigationController.navigationBar.barTintColor = kNightModeNavigationColor;
    } else {
        self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:248 / 255.0 green:78 / 255.0 blue:78 / 255.0 alpha:1];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"我的";
    [self createdTableview];
    
    // Do any additional setup after loading the view.
}

- (void)viewDidAppear:(BOOL)animated
{
    NSIndexPath *indexPath=[NSIndexPath indexPathForRow:1 inSection:1];
    
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"NightMode"] == YES){
        self.table.backgroundColor = kNightModeColor;
        self.navigationController.navigationBar.barTintColor = kNightModeNavigationColor;
    }
    
    else{
        self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:248 / 255.0 green:78 / 255.0 blue:78 / 255.0 alpha:1];
        self.table.backgroundColor = [UIColor whiteColor];
    }
    
    [self.table reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationNone];
}

- (void)createdTableview
{
    self.table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kHeight - 64) style:UITableViewStyleGrouped];
    
    [self.table registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCell"];
    [self.table registerClass:[SwitchTableViewCell class] forCellReuseIdentifier:@"SwitchTableViewCell"];
    
    UIImageView *view = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kHeight / 3.5)];
    view.backgroundColor = [UIColor cyanColor];
    view.image = [UIImage imageNamed:@"u=1895521771,657246832&fm=11&gp=0.jpg"];
    
    self.table.tableHeaderView = view;
    
    self.table.delegate = self;
    self.table.dataSource = self;
    
    [self.view addSubview:self.table];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 1) {
        return 2;
    }
    
    else if (section == 0){
        return 2;
    }
    
    else{
        return 2;
    }
}

//- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
//{
//    return CGFLOAT_MIN;
//}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 1 && indexPath.row == 0) {
        SwitchTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SwitchTableViewCell" forIndexPath:indexPath];
//        NSLog(@"******");
        cell.delegate = self;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
//        switch (indexPath.row) {
//            case 0:
//                cell.title = @"接收消息";
//                cell.imgname = @"iconfont-qunfengxinxituisong";
//                break;
//            case 1:
                cell.title = @"夜间模式";
                cell.imgname = @"iconfont-yueliang02";
//                break;
        
//            default:
//                break;
//        }
        
        return cell;
    }
    
    else if (indexPath.section == 1 && indexPath.row == 1){
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell" forIndexPath:indexPath];
        if ([[NSUserDefaults standardUserDefaults] boolForKey:@"NightMode"] == YES){
            cell.backgroundColor = kNightModeColor;
            cell.textLabel.textColor = [UIColor whiteColor];
        }
        else{
            cell.backgroundColor = [UIColor whiteColor];
            cell.textLabel.textColor = [UIColor blackColor];
        }
        NSString *path = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES).lastObject;
        path = [path stringByAppendingPathComponent:@"Caches"];
        
        cell.textLabel.text = [NSString stringWithFormat:@"清除缓存  %.2fM",[self folderSizeAtPath:path]];
        
        cell.imageView.image = [UIImage imageNamed:@"iconfont-appxiugaiicon29.png"];
        return cell;
    }
    
    else{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell" forIndexPath:indexPath];
        if ([[NSUserDefaults standardUserDefaults] boolForKey:@"NightMode"] == YES)
        {
            cell.backgroundColor = kNightModeColor;
            cell.textLabel.textColor = [UIColor whiteColor];
        }
        else{
            cell.backgroundColor = [UIColor whiteColor];
            cell.textLabel.textColor = [UIColor blackColor];
        }
    [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
    cell.imageView.contentMode = UIViewContentModeScaleAspectFit;
        cell.selectionStyle = UITableViewCellSelectionStyleBlue;
    if (indexPath.section == 0) {
        switch (indexPath.row) {
            case 0:
                cell.textLabel.text = @"收藏的礼物";
                cell.imageView.image = [UIImage imageNamed:@"iconfont-gift"];
                break;
            case 1:
                cell.textLabel.text = @"喜欢的攻略";
                cell.imageView.image = [UIImage imageNamed:@"iconfont-gonglve"];
                break;
            default:
                break;
        }
    }
    
    else if (indexPath.section == 2){
        switch (indexPath.row) {
            case 0:
                cell.textLabel.text = @"意见反馈";
                cell.imageView.image = [UIImage imageNamed:@"iconfont-appxiugaiicon31"];
                break;
            case 1:
                cell.textLabel.text = @"关于我们";
                cell.imageView.image = [UIImage imageNamed:@"iconfont-guanyu"];
                break;
                
            default:
                break;
        }
    }
        return cell;
    }
}

#pragma mark 代理方法

-(void)cleanAction
{
    //获取完整路径
    NSString *path = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES).lastObject;
    path = [path stringByAppendingPathComponent:@"Caches"];
    NSFileManager *manager = [NSFileManager defaultManager];
    if ([manager fileExistsAtPath:path]) {
        NSArray *fileNameAarray = [manager subpathsAtPath:path];
        for (NSString *fileName in fileNameAarray) {
            //拼接绝对路径
            NSString *absolutePath = [path stringByAppendingPathComponent:fileName];
            
            //通过文件管理者删除文件
            [manager removeItemAtPath:absolutePath error:nil];
        }
    }
    
    UITableViewCell *cell = [self tableView:self.table cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:2]];
    
    cell.textLabel.text = [NSString stringWithFormat:@"%.2fM",[self folderSizeAtPath:path]];
    
    //    _label.text = [NSString stringWithFormat:@"%.2fM",[self folderSizeAtPath:path]];
}

-(long long)fileSizeAtPath:(NSString *)path
{
    NSFileManager *manager = [NSFileManager defaultManager];
    if ([manager fileExistsAtPath:path]) {
        return [[manager attributesOfItemAtPath:path error:nil]fileSize];
    }
    return 0;
}

-(float)folderSizeAtPath:(NSString *)path
{
    NSFileManager *manager = [NSFileManager defaultManager];
    if (![manager fileExistsAtPath:path]) {
        return 0;
    }
    //根据路径获取文件夹里面元素集合
    //获取集合类型的枚举类型
    NSEnumerator *enumeratot = [[manager subpathsAtPath:path] objectEnumerator];
    //每次遍历得到的文件名
    //文件夹的大小
    NSString *fileName = [NSString string];
    float folderSize = 0;
    while ((fileName = [enumeratot nextObject])!= nil) {
        NSString *absolutePath = [path stringByAppendingPathComponent:fileName];
        folderSize += [self fileSizeAtPath:absolutePath];
    }
    return folderSize/(1024*1024.0);
}



- (void)sendSwich:(BOOL)swich AndTitle:(NSString *)title
{
    if ([title isEqualToString:@"夜间模式"]) {
        [self.table reloadData];
        if (swich == YES) {
            self.table.backgroundColor = [UIColor whiteColor];
            self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:248 / 255.0 green:78 / 255.0 blue:78 / 255.0 alpha:1];
        }
        else{
            self.table.backgroundColor = kNightModeColor;
            self.navigationController.navigationBar.barTintColor = kNightModeNavigationColor;
        }
        
        NSIndexPath *index = [NSIndexPath indexPathForItem:0 inSection:1];
        [self.table reloadRowsAtIndexPaths:@[index] withRowAnimation:UITableViewRowAnimationNone];
    }
    else{
        NSLog(@"%d    %@",swich,title);
    }
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    if (indexPath.section == 1 && indexPath.row == 1) {
        [self cleanAction];
        [self.table reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationNone];
    }
    
    else if (indexPath.section == 0){
        ListViewController *list = [[ListViewController alloc] init];
        if (indexPath.row == 1) {
            list.usein = @"post";
        }
        else{
            list.usein = @"item";
        }
        
        [self.navigationController pushViewController:list animated:YES];
    }
    
    else if (indexPath.section == 2 && indexPath.row == 0){
        ConnectOurViewController *connect = [[ConnectOurViewController alloc] init];
        [self.navigationController pushViewController:connect animated:YES];
    }
    
    else if (indexPath.section == 2 && indexPath.row == 1){
        AboutOurViewController *about = [[AboutOurViewController alloc] init];
        [self.navigationController pushViewController:about animated:YES];
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
